
#include "_gfx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

#include "listbox.h"
#include "_listbox.h"
#include "label.h"
#include "window.h"
#include "_font.h"

#ifdef __DEBUG__
#define __E(fmt, args...) fprintf(stderr, "LISTBOX ERROR [%d]: " fmt, __LINE__, ## args)
#define __W(fmt, args...) fprintf(stderr, "LISTBOX WARNING [%d]: " fmt, __LINE__, ## args)
#else
#define __E(fmt, args...)
#define __W(fmt, args...)
#endif

static listbox lists[LISTBOX_MAX_NUMBER];

signed int listbox_create(unsigned int x, unsigned int y, unsigned int width,
        unsigned int height, unsigned int r, unsigned int g, unsigned int b,
        unsigned char border, unsigned char font_height, unsigned int font_r,
        unsigned int font_g, unsigned int font_b) {

    signed int lbId = -1;
    signed int i;
    for (i = 0; i < LISTBOX_MAX_NUMBER; i++) {
        if (lists[i].initialized == 0) {
            lbId = i;
            break;
        }
    }

    if (lbId == LISTBOX_FAILURE) {
        __E("Nao e possivel criar mais listboxes. Numero maximo.\n");
        return LISTBOX_FAILURE;
    } else {
        if (height < font_height + 4) {
            __E("Tamanho da fonte maior que o listbox.\n");
            return LISTBOX_FAILURE;
        }

        lists[lbId].x = x;
        lists[lbId].y = y;
        lists[lbId].h = height;
        lists[lbId].w = width;
        lists[lbId].bg_r = r;
        lists[lbId].bg_g = g;
        lists[lbId].bg_b = b;
        lists[lbId].font_r = font_r;
        lists[lbId].font_g = font_g;
        lists[lbId].font_b = font_b;
        lists[lbId].r_elm = (lists[lbId].bg_r - DEPTH_OFFSET < 0) ? (lists[lbId].bg_r + DEPTH_OFFSET) : (lists[lbId].bg_r - DEPTH_OFFSET);
        lists[lbId].g_elm = (lists[lbId].bg_g - DEPTH_OFFSET < 0) ? (lists[lbId].bg_g + DEPTH_OFFSET) : (lists[lbId].bg_g - DEPTH_OFFSET);
        lists[lbId].b_elm = (lists[lbId].bg_b - DEPTH_OFFSET < 0) ? (lists[lbId].bg_b + DEPTH_OFFSET) : (lists[lbId].bg_b - DEPTH_OFFSET);
        lists[lbId].font_height = font_height;
        lists[lbId].border = border;
        lists[lbId].numberElements = 0;
        lists[lbId].actualElement_selected = -1;
        lists[lbId].numberLabels = (int) ((height - border * 2) / (font_height + ELEMENT_SPACE));
        lists[lbId].firstVisibleElement = -1;
        lists[lbId].lastVisibleElement = -1;
        lists[lbId].visible = 1;
        lists[lbId].id = lbId;
        lists[lbId].windowId = -1;
        lists[lbId].winp = NULL;
        lists[lbId].next = &lists[lbId];
        lists[lbId].previous = &lists[lbId];

        signed int aux;
        for (aux = 0; aux < MAX_NUMBER_LISTBOX_ELEMENTS; aux++) {
            lists[lbId].elements[aux].labelId = -1;
            lists[lbId].elements[aux].listboxId = lbId;
            strncpy(lists[lbId].elements[aux].text, "", MAX_LISTBOX_TEXT);
            lists[lbId].elements[aux].position = -1;
        }

        lists[lbId].labels = malloc(lists[lbId].numberLabels * sizeof (int));
        if (lists[lbId].labels == NULL) {
            __E("Nao foi possivel criar o listbox\n");
            return LISTBOX_FAILURE;
        }

        for (aux = 0; aux < lists[lbId].numberLabels; aux++) {
            lists[lbId].labels[aux] = label_create(lists[lbId].x + lists[lbId].border + ELEMENT_SPACE,
                    lists[lbId].y + lists[lbId].border + ELEMENT_SPACE / 2 + (lists[lbId].font_height + ELEMENT_SPACE) * aux,
                    lists[lbId].font_height, "", lists[lbId].font_r, lists[lbId].font_g, lists[lbId].font_b);
            if (lists[lbId].labels[aux] == LABEL_FAILURE) {
                __E("Nao foi possivel criar o listbox.\n");
                return LISTBOX_FAILURE;
            }
        }

        lists[lbId].initialized = 1;
        lists[lbId].redraw = 1;
        return lbId;
    }
}

signed int listbox_destroy(signed int listboxId) {
    if (!lists[listboxId].initialized) {
        __E("Listbox nao pode ser destruido: listbox nao foi criado.");
        return LISTBOX_FAILURE;
    }

    lists[listboxId].x = 0;
    lists[listboxId].y = 0;
    lists[listboxId].h = 0;
    lists[listboxId].w = 0;
    lists[listboxId].bg_r = 0;
    lists[listboxId].bg_g = 0;
    lists[listboxId].bg_b = 0;
    lists[listboxId].font_r = 0;
    lists[listboxId].font_g = 0;
    lists[listboxId].font_b = 0;
    lists[listboxId].r_elm = 0;
    lists[listboxId].g_elm = 0;
    lists[listboxId].b_elm = 0;
    lists[listboxId].firstVisibleElement = -1;
    lists[listboxId].lastVisibleElement = -1;
    lists[listboxId].actualElement_selected = -1;
    lists[listboxId].numberElements = 0;
    lists[listboxId].redraw = 0;
    signed int aux;
    for (aux = 0; aux < lists[listboxId].numberLabels; aux++) {
        label_destroy(lists[listboxId].labels[aux]);
    }
    lists[listboxId].numberLabels = 0;
    free(lists[listboxId].labels);
    lists[listboxId].visible = 0;
    lists[listboxId].border = 0;
    lists[listboxId].font_height = 0;
    for (aux = 0; aux < MAX_NUMBER_LISTBOX_ELEMENTS; aux++) {
        lists[listboxId].elements[aux].labelId = -1;
        lists[listboxId].elements[aux].listboxId = listboxId;
        strncpy(lists[listboxId].elements[aux].text, "", MAX_LISTBOX_TEXT);
        lists[listboxId].elements[aux].position = -1;
    }

    listbox_delFromWindow(listboxId);
    lists[listboxId].initialized = 0;
    return LISTBOX_SUCCESS;
}

/* Desenha o listbox todo no buffer da sua janela correspondente */
signed int listbox_draw(signed int listboxId) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            if (lists[listboxId].visible && lists[listboxId].redraw) {

                /* Desenhar o fundo do listbox */
                gfx_render_box(lists[listboxId].w, lists[listboxId].h, lists[listboxId].x,
                        lists[listboxId].y, lists[listboxId].border, lists[listboxId].winp,
                        lists[listboxId].bg_r, lists[listboxId].bg_g, lists[listboxId].bg_b);

                int aux;
                for (aux = 0; aux + lists[listboxId].firstVisibleElement <= lists[listboxId].lastVisibleElement; aux++) {
                    lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].position = aux;
                    lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].labelId = lists[listboxId].labels[aux];
                    if (aux + lists[listboxId].firstVisibleElement == lists[listboxId].actualElement_selected) {
                        fill_rectangle(lists[listboxId].x + lists[listboxId].border,
                                lists[listboxId].y + lists[listboxId].border + 2 + (lists[listboxId].font_height + ELEMENT_SPACE) * lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].position,
                                lists[listboxId].w - lists[listboxId].border * 2, lists[listboxId].font_height + ELEMENT_SPACE,
                                color_convert(lists[listboxId].r_elm, lists[listboxId].g_elm, lists[listboxId].b_elm),
                                lists[listboxId].winp);
                    }

                    if (font_getStringWidth(lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].text, lists[listboxId].font_height) > lists[listboxId].w) {
                        __E("Erro ao exibir os elementos do listbox.\n");
                        return LISTBOX_FAILURE;
                    }

                    if (label_setText(lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].labelId,
                            lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].text) == LABEL_FAILURE) {
                        __E("Erro ao exibir os elementos do listbox.\n");
                        return LISTBOX_FAILURE;
                    }
                    if (label_draw(lists[listboxId].elements[aux + lists[listboxId].firstVisibleElement].labelId) == LABEL_FAILURE) {
                        __E("Erro ao exibir os elementos do listbox.\n");
                        return LISTBOX_FAILURE;
                    }
                }

                return LISTBOX_SUCCESS;
            } else return LISTBOX_SUCCESS;
        } else {
            __E("Nenhuma tela associada ao listbox.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

// Só pode inserir, no máximo, uma posição após o último elemento atualmente presente no listbox.

signed int listbox_addElement(signed int listboxId, unsigned char *elementText, unsigned int elementIndex) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].numberElements < MAX_NUMBER_LISTBOX_ELEMENTS) {
            if (elementIndex <= lists[listboxId].numberElements) {
                if (lists[listboxId].numberElements == 0) {
                    lists[listboxId].firstVisibleElement = 0;
                    lists[listboxId].lastVisibleElement = 0;

                } else {
                    if (lists[listboxId].numberElements > 1) {
                        int i;
                        for (i = lists[listboxId].numberElements - 1; i >= (signed int) elementIndex; i--) {
                            //printf("i: %d\n", i);
                            lists[listboxId].elements[i + 1] = lists[listboxId].elements[i];
                        }
                    } else {
                        if (elementIndex == 0) {
                            lists[listboxId].elements[1] = lists[listboxId].elements[0];
                        }
                    }
                    if (lists[listboxId].numberElements < lists[listboxId].numberLabels) {
                        lists[listboxId].lastVisibleElement = lists[listboxId].numberElements;
                        lists[listboxId].elements[lists[listboxId].numberElements].position = lists[listboxId].numberElements;
                    }
                }

                strncpy(lists[listboxId].elements[elementIndex].text, elementText, MAX_LISTBOX_TEXT);

                lists[listboxId].redraw = 1;

                return lists[listboxId].numberElements++;
            } else {

                __E("Nao foi possivel adicionar o elemento. Posicao invalida.\n");
                return LISTBOX_FAILURE;
            }
        } else {

            __E("Nao foi possivel adicionar o elemento. Numero maximo.\n");
            return LISTBOX_FAILURE;
        }
    } else {

        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_removeElement(signed int listboxId, unsigned int elementIndex) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].numberElements > elementIndex) {
            int i;
            for (i = elementIndex; i < lists[listboxId].numberElements - 1; i++) {
                lists[listboxId].elements[i] = lists[listboxId].elements[i + 1];
            }

            // Elemento a ser removido está dentro da área visível?
            if (elementIndex >= lists[listboxId].firstVisibleElement
                    && elementIndex <= lists[listboxId].lastVisibleElement) {

                // Exitem elementos após o elemento a ser removido?
                if (lists[listboxId].lastVisibleElement == lists[listboxId].numberElements - 1) {
                    lists[listboxId].lastVisibleElement--;
                }



                // lists[listboxId].numberElements < lists[listboxId].numberLabels ????????????????
            }

            lists[listboxId].numberElements--;
            lists[listboxId].redraw = 1;
            return LISTBOX_SUCCESS;
        } else {
            __E("Nao foi possivel remover o elemento do listbox. Não há elemento na posicao especificada!\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_addToWindow(signed int listboxId, signed int listboxId_next, signed int windowId, window *winp) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId_next].winp == NULL) {
            signed int aux;
            lists[listboxId_next].next = lists[listboxId].next;
            lists[listboxId].next = &lists[listboxId_next];
            lists[listboxId_next].previous = &lists[listboxId];
            lists[listboxId_next].next->previous = &lists[listboxId_next];
            lists[listboxId_next].winp = winp;
            lists[listboxId_next].windowId = windowId;

            for (aux = 0; aux < lists[listboxId_next].numberLabels; aux++) {
                if (window_insertLabel(windowId, lists[listboxId_next].labels[aux]) == WINDOW_FAILURE) {
                    __E("Erro ao adicionar a tela ao listbox.\n");
                    return LISTBOX_FAILURE;
                }
            }

        } else {
            __E("Ja existe uma tela associada ao listbox.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }

    return LISTBOX_SUCCESS;
}

signed int listbox_delFromWindow(signed int listboxId) {
    if (!lists[listboxId].initialized) {
        __E("Listbox nao criado.\n");
        return LISTBOX_FAILURE;
    }

    if (lists[listboxId].winp == NULL) {
        return LISTBOX_SUCCESS;
    }

    if (lists[listboxId].next == &lists[listboxId]) {
        lists[listboxId].winp->firstListbox = -1;
        lists[listboxId].winp->actualListbox = -1;
    } else {
        if (lists[listboxId].winp->actualListbox == listboxId) {
            lists[listboxId].winp->actualListbox = lists[lists[listboxId].winp->actualListbox].previous->id;
        } else if (lists[listboxId].winp->firstListbox == listboxId) {
            lists[listboxId].winp->firstListbox = lists[lists[listboxId].winp->firstListbox].next->id;
        }

        lists[listboxId].previous->next = lists[listboxId].next;
        lists[listboxId].next->previous = lists[listboxId].previous;
    }

    signed int aux;
    for (aux = 0; aux < lists[listboxId].numberLabels; aux++) {
        window_removeLabel(lists[listboxId].windowId, lists[listboxId].labels[aux]);
    }

    lists[listboxId].previous = &lists[listboxId];
    lists[listboxId].next = &lists[listboxId];
    lists[listboxId].winp = NULL;
    lists[listboxId].windowId = -1;
    window_setComponentsRedraw(lists[listboxId].windowId);
    return LISTBOX_SUCCESS;
}

signed int listbox_nextElement(signed int listboxId) {
    if ((lists[listboxId].actualElement_selected + 1) < lists[listboxId].numberElements) {
        if (listbox_selectElement(listboxId, lists[listboxId].actualElement_selected + 1) == LISTBOX_FAILURE) {
            __E("Erro ao selecionar o proximo elemento.\n");
            return LISTBOX_FAILURE;
        }
    }
    return lists[listboxId].actualElement_selected;
}

signed int listbox_previousElement(signed int listboxId) {
    if (lists[listboxId].actualElement_selected == -1) {
        if (listbox_selectElement(listboxId, lists[listboxId].numberElements - 1) == LISTBOX_FAILURE) {
            __E("Erro ao selecionar o elemento anterior.\n");
            return LISTBOX_FAILURE;
        }
    } else if ((lists[listboxId].actualElement_selected - 1) >= 0) {
        if (listbox_selectElement(listboxId, lists[listboxId].actualElement_selected - 1) == LISTBOX_FAILURE) {
            __E("Erro ao selecionar o elemento anterior.\n");
            return LISTBOX_FAILURE;
        }
    }
    return lists[listboxId].actualElement_selected;
}

signed int listbox_nextPage(signed int listboxId) {
    if (lists[listboxId].lastVisibleElement == lists[listboxId].numberElements - 1) {
        __E("Ja esta no fim.\n");
        return LISTBOX_LAST_PAGE;
    } else {
        if (lists[listboxId].lastVisibleElement + lists[listboxId].numberLabels > lists[listboxId].numberElements - 1) {
            lists[listboxId].firstVisibleElement = lists[listboxId].numberElements - lists[listboxId].numberLabels;
            lists[listboxId].lastVisibleElement = lists[listboxId].numberElements - 1;
        } else {
            lists[listboxId].firstVisibleElement = lists[listboxId].lastVisibleElement + 1;
            lists[listboxId].lastVisibleElement = lists[listboxId].firstVisibleElement + lists[listboxId].numberLabels - 1;
        }
        lists[listboxId].actualElement_selected = -1;
        lists[listboxId].redraw = 1;
        return LISTBOX_SUCCESS;
    }
}

signed int listbox_previousPage(signed int listboxId) {
    if (lists[listboxId].firstVisibleElement == 0) {
        __E("Ja esta no comeco.\n");
        return LISTBOX_FIRST_PAGE;
    } else {
        if (((int) (lists[listboxId].firstVisibleElement - lists[listboxId].numberLabels)) < 0) {
            lists[listboxId].firstVisibleElement = 0;
            lists[listboxId].lastVisibleElement = lists[listboxId].firstVisibleElement + lists[listboxId].numberLabels - 1;
        } else {
            lists[listboxId].firstVisibleElement = lists[listboxId].firstVisibleElement - lists[listboxId].numberLabels;
            lists[listboxId].lastVisibleElement = lists[listboxId].firstVisibleElement + lists[listboxId].numberLabels - 1;
        }

        lists[listboxId].actualElement_selected = -1;

        return LISTBOX_SUCCESS;
    }
}

signed int listbox_selectElement(signed int listboxId, signed int elementId) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            if (elementId >= lists[listboxId].numberElements || elementId < 0) {
                __E("Elemento nao foi criado.\n");
                return LISTBOX_FAILURE;
            }

            if (lists[listboxId].numberElements > 0) {

                if (elementId < lists[listboxId].firstVisibleElement) {
                    lists[listboxId].firstVisibleElement = elementId;
                    lists[listboxId].lastVisibleElement = elementId + lists[listboxId].numberLabels - 1;

                } else if (elementId > lists[listboxId].lastVisibleElement) {
                    lists[listboxId].lastVisibleElement = elementId;
                    lists[listboxId].firstVisibleElement = elementId - lists[listboxId].numberLabels + 1;
                }

                lists[listboxId].actualElement_selected = elementId;
                lists[listboxId].redraw = 1;
                return LISTBOX_SUCCESS;

            } else {
                __E("Nenhum elemento adicionado ao listbox.\n");
                return LISTBOX_FAILURE;
            }
        } else {
            __E("Nenhuma tela associada ao listbox.");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_unselectElement(signed int listboxId) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            lists[listboxId].actualElement_selected = -1;
            lists[listboxId].redraw = 1;
            return LISTBOX_SUCCESS;

        } else {
            __E("Nenhuma tela associada ao listbox.");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_getActualElementId(signed int listboxId) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            if (lists[listboxId].actualElement_selected != -1) return lists[listboxId].actualElement_selected;
            else {
                __E("Nenhum elemento selecionado.\n");
                return LISTBOX_FAILURE;
            }
        } else {
            __E("Nenhuma tela associada ao listbox.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_getTextElement(signed int listboxId, signed int elementId, unsigned char *text) {
    if (elementId >= lists[listboxId].numberElements || elementId < 0) {
        strcpy(text, "");
        __E("Elemento nao foi criado.\n");
        return LISTBOX_FAILURE;
    } else {
        strcpy(text, lists[listboxId].elements[elementId].text);
        return LISTBOX_SUCCESS;
    }
}

signed int listbox_next(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].next->id;
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_previous(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].previous->id;
    } else {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_setBorder(signed int listboxId, unsigned int border) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            lists[listboxId].border = border;
            lists[listboxId].redraw = 1;
            return LISTBOX_SUCCESS;
        } else {
            __E("Nenhuma tela associada ao listbox.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_setColor_selectedElement(signed int listboxId, unsigned int r, unsigned int g, unsigned int b) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            lists[listboxId].r_elm = r;
            lists[listboxId].g_elm = g;
            lists[listboxId].b_elm = b;
            lists[listboxId].redraw = 1;
            return LISTBOX_SUCCESS;
        } else {
            __E("Nenhuma tela associada ao listbox.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_setColor(signed int listboxId, unsigned int r, unsigned int g, unsigned int b) {
    if (lists[listboxId].initialized) {
        if (lists[listboxId].winp) {
            lists[listboxId].bg_r = r;
            lists[listboxId].bg_g = g;
            lists[listboxId].bg_b = b;
            lists[listboxId].redraw = 1;
            return LISTBOX_SUCCESS;
        } else {
            __E("Nenhuma tela associada ao listbox.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_getX(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].x;
    } else {
        __E("Listbox não foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_getY(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].y;
    } else {
        __E("Listbox não foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_getWidth(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].w;
    } else {
        __E("Listbox não foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_getHeight(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].h;
    } else {
        __E("Listbox não foi criado.\n");
        return LISTBOX_FAILURE;
    }
}

signed int listbox_setVisible(signed int listboxId, unsigned char visibility) {
    if (!lists[listboxId].initialized) {
        __E("Listbox nao foi criado.\n");
        return LISTBOX_FAILURE;
    }

    if (!lists[listboxId].winp) {
        __E("Nenhuma tela associada ao listbox.\n");
        return LISTBOX_FAILURE;
    }

    if (visibility != lists[listboxId].visible) {
        if (visibility) {
            lists[listboxId].redraw = 1;
        } else {
            window_setComponentsRedraw(lists[listboxId].windowId);
        }

        lists[listboxId].visible = visibility;
    }

    return LISTBOX_SUCCESS;
}

void listbox_setRedraw(signed int listboxId) {
    lists[listboxId].redraw = 1;
}

signed int listbox_getNumberElements(signed int listboxId) {
    if (lists[listboxId].initialized) {
        return lists[listboxId].numberElements;
    }

    return LISTBOX_FAILURE;
}

signed int listbox_setTextElement(signed int listboxId, unsigned int elementId, unsigned char *text) {
    if (lists[listboxId].initialized) {
        if (elementId < lists[listboxId].numberElements) {
            strncpy(lists[listboxId].elements[elementId].text, text, LABEL_MAX_TEXT);
            return LISTBOX_SUCCESS;
        } else {
            __E("Impossivel setar texto do elemento. Id do elemento do listbox invalido.\n");
            return LISTBOX_FAILURE;
        }
    } else {
        __E("Listbox nao criado.\n");
        return LISTBOX_FAILURE;
    }
}
